package com.binguo.api.controller;

import com.binguo.api.form.OrderForm;
import com.binguo.api.form.OrderZzgForm;
import com.binguo.api.form.ZzgCodePayForm;
import com.binguo.api.form.ZzgFacePayForm;
import com.binguo.api.result.Result;
import com.binguo.api.result.ResultGenerator;
import com.binguo.api.service.ApiOrderService;
import com.binguo.model.model.PayCard;
import com.binguo.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
 * @author yuritian
 * @Title: ZzgOrderController
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-09-1713:22
 */
@Api(tags = "收银端: 自助购")
@RestController
@RequestMapping
public class ZzgOrderController {

    @Autowired
    ApiOrderService apiOrderService;

    @Resource
    CustomerService customerService;

    @PostMapping(value = "/api/zzg/facePay", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建预订单", notes = "通过OrderForm")
    public Result facePay(@RequestBody @Valid ZzgFacePayForm form, HttpServletRequest request){

        apiOrderService.zzgFacePay(form.getCustomerID(),form.getShopID(),form.getRealFee());

        return ResultGenerator.genSuccessResult("success");
    }

    @PostMapping(value = "/api/zzg/hyCodePay", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建预订单", notes = "通过OrderForm")
    public Result hyCodePay(@RequestBody @Valid ZzgCodePayForm form, HttpServletRequest request){

        apiOrderService.zzgHyCodePay(form.getAuthCode(),form.getShopID(),form.getRealFee());

        return ResultGenerator.genSuccessResult("success");
    }

    @PostMapping(value = "/api/zzg/cardPay", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自助购实体卡支付", notes = "通过OrderRefundForm")
    public Result zzgCardPay(@RequestBody @Valid OrderZzgForm form) {
        PayCard payCard = customerService.getYpCard(form.getCardId(),form.getShopId());
        customerService.customerPay(payCard,form.getRealFee(),form.getOrderNo());
        return ResultGenerator.genSuccessResult();
    }
}
